package org.hhchat.leetcode.tree;

import org.hhchat.leetcode.dataStruct.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created this one by HMH on 2017/9/30.
 */
public class code501_easy {
    class Solution {
        public int[] findMode(TreeNode root) {
            list = new ArrayList<>();
            if (root == null) {
                return new int[0];
            }
            visit(root);
            int[] result = new int[list.size()];
            for(int i = 0 ;i < list.size();i++) {
                result[i] = list.get(i);
            }
            return result;
        }

        int prev = -1;
        int max=0;
        int count = 0;
        List<Integer> list;

        public void visit(TreeNode root) {
            if (root == null) {
                return;
            }
            visit(root.left);
            if (root.val == prev) {
                count++;
            }else{
                count = 1;
            }
            if (count > max) {
                max = count;
                list.clear();
                list.add(root.val);
            } else if (count == max) {
                list.add(root.val);
            }
            prev = root.val;
            visit(root.right);
        }
    }
    public static void main(String[] args){

    }
}
